package com.miss.spjyx

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.miss.spjyx.model.DeviceModel

/**
 * 设备识别adapter
 *
 * @author wl
 * @since 2019/08/11 19:01
 */
class DeviceAdapter(var context: Context) : RecyclerView.Adapter<DeviceAdapter.ViewHolder>() {

    var data: List<DeviceModel>? = null
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_dev, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, i: Int) {
        val res = data?.get(i)
        if (res != null) {
            holder.tv_num.text = (i + 1).toString()
            holder.tv_name.text = res.equipmentName
            holder.tv_serial.text = res.code
            holder.tv_sn.text = res.sn
            holder.tv_spm.text = res.rf
        }
    }

    override fun getItemCount(): Int {
        return data?.size ?: 0
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_num = itemView.findViewById<TextView>(R.id.tv_num)
        val tv_spm = itemView.findViewById<TextView>(R.id.tv_spm)
        val tv_name = itemView.findViewById<TextView>(R.id.tv_name)
        val tv_serial = itemView.findViewById<TextView>(R.id.tv_serial)
        val tv_sn = itemView.findViewById<TextView>(R.id.tv_sn)
    }
}
